package com.vrv.netmgr.alarm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.vrv.common.GlobalConstants;
import com.vrv.common.UrlPrefix;
import com.vrv.common.listview.ListViewAdapter;
import com.vrv.common.listview.ListViewUtil;
import com.vrv.common.util.HttpClientToServer;
import com.vrv.common.util.JsonUtil;
import com.vrv.netmgr.R;
import com.vrv.netmgr.alarm.detail.DetailOpenIONetActivity;
import com.vrv.netmgr.assets.AdvanceSofterwareQueryActivity;
import com.vrv.netmgr.vo.SerializableOpenIONet;

/**
 * @Description 同时连接内外网
 * @author gaopei
 * @date
 *
 */
@SuppressLint("HandlerLeak")
public class OpenIONetActivity extends Activity {

    // 存储数据的数组列表
    private ArrayList<HashMap<String, Object>> listData = new ArrayList<HashMap<String, Object>>();
    private ListView listView;
    private ListAdapter adapter = new ListAdapter();
    private ListViewItemClick listItemClk = new ListViewItemClick();
    private String UNIT_NAME;
    private String DEP_NAME;
    private String USER_NAME;
    private String TEL;
    private String IP_ADDRESS;
    private String VIOLATION_IP;
    private String NET_START_TIME;
    private String NET_END_TIME;
    private String NET_DURATION;
    private String ALARM_TYPE;
    private String LOGIN_USER;
    private String ALARM_TIME;
    private String LOOK_FLAG;
    // 列表的页脚视图
    private View loadMoreView;
    /**
     * 等待对话框
     */
    private ProgressDialog progressDialog;
    /**
     * 查询
     */
    private static final int QUERY = 3;
    private static final int NET_ERR = 4;
    private static final int QRY_NET_DUR_ZERO = 5;
    private static final int QRY_NET_DUR_NOT_ZERO = 6;
    private static final int QUERY_BY_IP = 7;
    // 查询到的记录总数
    public int RECORD_COUNT;
    // 当前页号，从0开始算
    public int CUR_INDEX;
    // 通过点击按钮查询标记
    private boolean qryByBtnFlag = false;
    // PopWindow检索条件相关
    private ImageButton moreFuncBtn;
    private PopupWindow pwMyPopWindow;
    private ListView lvPopupList;
    public final static String KEY_pageNum = "pageNum";
    public final static String KEY_byDeviceIP = "byDeviceIP";
    public final static String KEY_deviceIP = "deviceIP";
    public final static String KEY_pageDown = "pageDown";
    // 当前查询类型
    private boolean curNetDurIsZero = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.assets_list_page);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tools_title);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        init();
        progressDialog = ProgressDialog.show(OpenIONetActivity.this, "", "查询中...", true, true);

        new Thread(new Runnable() {
            @Override
            public void run() {
                Map<String, Object> argsMap = new HashMap<String, Object>();
                argsMap.put(KEY_pageNum, 0);
                argsMap.put(KEY_byDeviceIP, false);
                argsMap.put(KEY_deviceIP, null);
                getDataFromServer(argsMap);
                handler.sendEmptyMessage(QUERY);
            }
        }).start();
    }

	/**listview背景设置*/
	private LinearLayout listBackView;
	
	/**
	 * 处理器
	 */
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            progressDialog.dismiss();
            switch (msg.what) {
            case QUERY:
                findViews();
				/** listview背景设置 */
				listBackView.setVisibility(listView.getVisibility());
				break;
            case NET_ERR:
                Toast.makeText(OpenIONetActivity.this, "网络连接失败", Toast.LENGTH_LONG).show();
                break;
            case QRY_NET_DUR_ZERO:
                qryByBtnFlag = false;
                if (RECORD_COUNT == 0) {
                    listView.setVisibility(View.INVISIBLE);
                    break;
                } else
                    listView.setVisibility(View.VISIBLE);
                if (RECORD_COUNT > ListViewUtil.PAGESIZE) {
                    Map<String, Object> argsMap = new HashMap<String, Object>();
                    argsMap.put(KEY_byDeviceIP, false);
                    argsMap.put(KEY_deviceIP, null);
                    loadMorePage(argsMap);
                }
                adapter.setNetDur(true);
                listItemClk.setNetDur(true);
                adapter.clear();
                initializeAdapter();
                adapter.notifyDataSetChanged();
				/** listview背景设置 */
				listBackView.setVisibility(listView.getVisibility());
				break;
            case QRY_NET_DUR_NOT_ZERO:
                qryByBtnFlag = false;
                if (RECORD_COUNT == 0) {
                    listView.setVisibility(View.INVISIBLE);
                    break;
                } else
                    listView.setVisibility(View.VISIBLE);
                if (RECORD_COUNT > ListViewUtil.PAGESIZE) {
                    Map<String, Object> argsMap = new HashMap<String, Object>();
                    argsMap.put(KEY_byDeviceIP, false);
                    argsMap.put(KEY_deviceIP, null);
                    loadMorePage(argsMap);
                }
                adapter.setNetDur(false);
                listItemClk.setNetDur(false);
                adapter.clear();
                initializeAdapter();
                adapter.notifyDataSetChanged();
				/** listview背景设置 */
				listBackView.setVisibility(listView.getVisibility());
				break;
            case QUERY_BY_IP:
                qryByBtnFlag = true;
                if (RECORD_COUNT > 0) {
                    listView.removeFooterView(loadMoreView);
                    listView.setVisibility(View.VISIBLE);
                    if (RECORD_COUNT > ListViewUtil.PAGESIZE) {
                        String deviceIP = msg.getData().getString("deviceIP");
                        Map<String, Object> argsMap = new HashMap<String, Object>();
                        argsMap.put(KEY_byDeviceIP, true);
                        argsMap.put(KEY_deviceIP, deviceIP);
                        loadMorePage(argsMap);
                    }
                    initializeAdapter();
                    adapter.notifyDataSetChanged();
                } else
                    listView.setVisibility(View.GONE);
				/** listview背景设置 */
				listBackView.setVisibility(listView.getVisibility());
				break;
            }
        }
    };

    private void init() {
        setTitle();
        UNIT_NAME = this.getString(R.string.unit_name);
        DEP_NAME = this.getString(R.string.dep_name);
        USER_NAME = this.getString(R.string.userName);
        TEL = this.getString(R.string.tel);
        IP_ADDRESS = this.getString(R.string.ip_address);
        VIOLATION_IP = this.getString(R.string.violation_ip);
        NET_START_TIME = this.getString(R.string.net_start_time);
        NET_END_TIME = this.getString(R.string.net_end_time);
        NET_DURATION = this.getString(R.string.net_duration);
        ALARM_TYPE = this.getString(R.string.alarm_type);
        LOGIN_USER = this.getString(R.string.login_user);
        ALARM_TIME = this.getString(R.string.alarm_time);
        LOOK_FLAG = this.getString(R.string.look_flag);
        
		listBackView = (LinearLayout) findViewById(R.id.list_background);
    }

    /**
     * 初始化ListView的适配器
     */
    public void initializeAdapter() {

        for (int i = 0; i < listData.size(); i++)
            adapter.addItem(listData.get(i));
    }

    private class ListViewItemClick implements AdapterView.OnItemClickListener {
        @SuppressWarnings("unused")
        private boolean netDurIsZero;

        public void setNetDur(boolean netDurIsZero) {
            this.netDurIsZero = netDurIsZero;
        }

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            HashMap<String, Object> hashMap = adapter.getData(arg2);
            //
            SerializableOpenIONet serializable = new SerializableOpenIONet();
            serializable.setUNIT_NAME(hashMap.get(UNIT_NAME).toString());
            serializable.setDEP_NAME(hashMap.get(DEP_NAME).toString());
            serializable.setUSER_NAME(hashMap.get(USER_NAME).toString());
            serializable.setTEL(hashMap.get(TEL).toString());
            serializable.setIP_ADDRESS(hashMap.get(IP_ADDRESS).toString());
            serializable.setVIOLATION_IP(hashMap.get(VIOLATION_IP).toString());
            serializable.setNET_START_TIME(hashMap.get(NET_START_TIME).toString());
            serializable.setNET_END_TIME(hashMap.get(NET_END_TIME).toString());
            serializable.setNET_DURATION(hashMap.get(NET_DURATION).toString());
            serializable.setALARM_TYPE(hashMap.get(ALARM_TYPE).toString());
            serializable.setLOGIN_USER(hashMap.get(LOGIN_USER).toString());
            serializable.setALARM_TIME(hashMap.get(ALARM_TIME).toString());
            serializable.setLOOK_FLAG(hashMap.get(LOOK_FLAG).toString());
            //
            Intent intent = new Intent(OpenIONetActivity.this, DetailOpenIONetActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("info", serializable);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    }

    public void loadMorePage(Map<String, Object> argsMap) {
        final boolean byDeviceIP = (Boolean) argsMap.get(KEY_byDeviceIP);
        final String deviceIP = (String) argsMap.get(KEY_deviceIP);
        loadMoreView = getLayoutInflater().inflate(R.layout.list_page_load, null);
        final Button loadNextButton = (Button) loadMoreView.findViewById(R.id.loadNextButton);
        final Button loadPreButton = (Button) loadMoreView.findViewById(R.id.loadPreButton);
        loadNextButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (loadNextButton.getText().equals("查看下一页")) {
                    loadNextButton.setText("正在加载中..."); // 设置按钮文字
                    loadNextButton.setEnabled(false);
                    new Handler().postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            Map<String, Object> argsMap = new HashMap<String, Object>();
                            argsMap.put(KEY_pageDown, true);
                            argsMap.put(KEY_byDeviceIP, byDeviceIP);
                            argsMap.put(KEY_deviceIP, deviceIP);
                            loadMoreData(argsMap);
                            adapter.notifyDataSetChanged();
                            loadNextButton.setText("查看下一页"); // 恢复按钮文字
                            if ((CUR_INDEX + 1) * ListViewUtil.PAGESIZE >= RECORD_COUNT)
                                loadNextButton.setVisibility(View.GONE);
                            if (CUR_INDEX == 1)
                                loadPreButton.setVisibility(View.VISIBLE);
                            loadNextButton.setEnabled(true);
                        }
                    }, 0);
                }
            }
        });
        loadPreButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (loadPreButton.getText().equals("查看上一页")) {
                    loadPreButton.setText("正在加载中..."); // 设置按钮文字
                    loadPreButton.setEnabled(false);
                    new Handler().postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            Map<String, Object> argsMap = new HashMap<String, Object>();
                            argsMap.put(KEY_pageDown, false);
                            argsMap.put(KEY_byDeviceIP, byDeviceIP);
                            argsMap.put(KEY_deviceIP, deviceIP);
                            loadMoreData(argsMap);
                            adapter.notifyDataSetChanged();
                            loadPreButton.setText("查看上一页"); // 恢复按钮文字
                            if (CUR_INDEX == 0)
                                loadPreButton.setVisibility(View.GONE);
                            if ((CUR_INDEX + 2) * ListViewUtil.PAGESIZE >= RECORD_COUNT)
                                loadNextButton.setVisibility(View.VISIBLE);
                            loadPreButton.setEnabled(true);
                        }
                    }, 0);
                }
            }
        });
        listView.addFooterView(loadMoreView); // 设置列表底部视图
    }

    /*
     * 加载更多数据
     */
    private void loadMoreData(Map<String, Object> argsMap) {
        int index;
        boolean pageDown = (Boolean) argsMap.get(KEY_pageDown);
        boolean byDeviceIP = (Boolean) argsMap.get(KEY_byDeviceIP);
        String deviceIP = (String) argsMap.get(KEY_deviceIP);
        if (pageDown)
            index = CUR_INDEX + 1;
        else
            index = CUR_INDEX - 1;
        listData.clear();
        adapter.clear();
        Map<String, Object> myMap = new HashMap<String, Object>();
        myMap.put(KEY_pageNum, index);
        myMap.put(KEY_byDeviceIP, byDeviceIP);
        myMap.put(KEY_deviceIP, deviceIP);
        getDataFromServer(myMap);
        int datasize = listData.size();
        for (int i = 0; i < datasize; i++)
            adapter.addItem(listData.get(i));
    }

    // 查询所有数据
    public void getDataFromServer(Map<String, Object> argsMap) {
        int pageNum = (Integer) argsMap.get(KEY_pageNum);
        boolean byDeviceIP = (Boolean) argsMap.get(KEY_byDeviceIP);
        String deviceIP = (String) argsMap.get(KEY_deviceIP);
        Map<String, String> map = new HashMap<String, String>();
        map.put(GlobalConstants.PAGE_NUM, pageNum + "");
        map.put(GlobalConstants.ERR_TYPE, "0");
        map.put(GlobalConstants.ERR_MSG, GlobalConstants.OPEN_IO_NET);
        map.put(GlobalConstants.NET_DURATION, curNetDurIsZero + "");
        if (byDeviceIP)
            map.put(GlobalConstants.DEVICE_IP, deviceIP);
        boolean retVal = qrynetDurIsZero(map, curNetDurIsZero);
        if (!retVal)
            handler.sendEmptyMessage(NET_ERR);
    }

    public boolean qrynetDurIsZero(Map<String, String> map, boolean netDurIsZero) {
        boolean retVal = false;
        HttpClientToServer httpClientToServer = new HttpClientToServer();
        String urlPrefix = new UrlPrefix(getApplicationContext()).getUrlPrefix();
        String urlAddr = urlPrefix + "/errorMessage/jsonList.do";
        String response = httpClientToServer.doPost(urlAddr, map);
        if (HttpClientToServer.DO_POST_ERR.equals(response) || HttpClientToServer.DO_POST_EXCEPT.equals(response))
            return retVal;
        try {
            JSONTokener jsonParser = new JSONTokener(response);
            JSONObject jsonObj = (JSONObject) jsonParser.nextValue();
            retVal = jsonObj.getBoolean("success");
            if (!retVal)
                return retVal;
            int count = jsonObj.getInt("totalRecords");
            RECORD_COUNT = count;
            if (count < 1)
                return retVal;
            JSONArray jArr = jsonObj.getJSONArray("records");
            int jArrLen = jArr.length();
            if (jArrLen > 0)
                CUR_INDEX = Integer.parseInt(map.get(GlobalConstants.PAGE_NUM));
            for (int i = 0; i < jArr.length(); i++) {
                JSONObject jObj = (JSONObject) jArr.opt(i);
                HashMap<String, Object> myMap = new HashMap<String, Object>();
                myMap.put(UNIT_NAME, JsonUtil.getJsonString(jObj, "deptName"));
                myMap.put(DEP_NAME, JsonUtil.getJsonString(jObj, "officeName"));
                myMap.put(USER_NAME, JsonUtil.getJsonString(jObj, "userName"));
                myMap.put(TEL, JsonUtil.getJsonString(jObj, "tel"));
                myMap.put(IP_ADDRESS, JsonUtil.getJsonString(jObj, "ipaddress"));
                myMap.put(VIOLATION_IP, JsonUtil.getJsonString(jObj, "ipaddress1"));
                myMap.put(NET_START_TIME, "null".equals(JsonUtil.getJsonString(jObj, "startTime")) ? "null" : JsonUtil.getJsonString(jObj, "startTime"));
                myMap.put(NET_END_TIME, "null".equals(JsonUtil.getJsonString(jObj, "endTime")) ? "null" : JsonUtil.getJsonString(jObj, "endTime"));
                myMap.put(NET_DURATION, JsonUtil.getJsonString(jObj, "duration") + "分钟");
                myMap.put(ALARM_TYPE, "在线上网(同时连接内外网)");
                myMap.put(LOGIN_USER, JsonUtil.getJsonString(jObj, "authUserName"));
                myMap.put(ALARM_TIME, "null".equals(JsonUtil.getJsonString(jObj, "errorTime")) ? "null" : JsonUtil.getJsonString(jObj, "errorTime"));
                myMap.put(LOOK_FLAG, "1".equals(JsonUtil.getJsonString(jObj, "lookFlag")) ? "已查看" : "未查看");
                listData.add(myMap);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return retVal;
    }

    public void setTitle() {
        TextView title = (TextView) findViewById(R.id.title_text);
        title.setText("同时连接内外网");

        /**
         * 返回按钮
         */
        ImageButton backBtn = (ImageButton) findViewById(R.id.backBtn);
        backBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    public void findViews() {
        if (listView == null) {
            // 搜索输入框
            EditText edit = (EditText) findViewById(R.id.assets_conditionQry);
            // 搜索按钮
            ImageView search_btn = (ImageView) findViewById(R.id.search_go_btn);
            edit.setHint(R.string.input_device_ip);
            // edit.addTextChangedListener(new MyTextWatcher(edit, search_btn));
            search_btn.setOnClickListener(new ClickSearchBtnListener(edit, search_btn));
            iniPopupWindow();
            // 更多操作按钮
            moreFuncBtn = (ImageButton) findViewById(R.id.moreFunctionBtn);
            moreFuncBtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (pwMyPopWindow.isShowing()) {
                        pwMyPopWindow.dismiss();// 关闭
                    } else {
                        pwMyPopWindow.showAsDropDown(moreFuncBtn);// 显示
                    }
                }
            });
            listView = (ListView) findViewById(R.id.assets_lv_id);
        }
        if (RECORD_COUNT > 0) {
            listView.removeFooterView(loadMoreView);
            listView.setVisibility(View.VISIBLE);
            if (RECORD_COUNT > ListViewUtil.PAGESIZE) {
                Map<String, Object> argsMap = new HashMap<String, Object>();
                argsMap.put(KEY_byDeviceIP, false);
                argsMap.put(KEY_deviceIP, null);
                loadMorePage(argsMap);
            }
            adapter.setNetDur(true);
            initializeAdapter();
            if (listView.getAdapter() == null)
                listView.setAdapter(adapter);
            else
                adapter.notifyDataSetChanged();
            listItemClk.setNetDur(true);
            listView.setOnItemClickListener(listItemClk);
        }
    }

    private class ClickSearchBtnListener implements View.OnClickListener {
        EditText edit;
        ImageView search_btn;

        ClickSearchBtnListener(EditText edit, ImageView search_btn) {
            this.edit = edit;
            this.search_btn = search_btn;
        }

        @Override
        public void onClick(View v) {
            final String editVal = edit.getText().toString();
            listData.clear();
            adapter.clear();
            progressDialog = ProgressDialog.show(OpenIONetActivity.this, "", "查询中...", true, true);
            search_btn.setEnabled(false);
            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    Map<String, Object> argsMap = new HashMap<String, Object>();
                    argsMap.put(KEY_pageNum, 0);
                    argsMap.put(KEY_byDeviceIP, true);
                    argsMap.put(KEY_deviceIP, editVal);
                    getDataFromServer(argsMap);
                    Message msg = new Message();
                    msg.what = QUERY_BY_IP;
                    Bundle data = new Bundle();
                    data.putString("deviceIP", editVal);
                    msg.setData(data);
                    handler.sendMessage(msg);
                    search_btn.setEnabled(true);
                }
            });
        }
    }

    // private class MyTextWatcher implements TextWatcher{
    // EditText edit;
    // ImageButton search_btn;
    //
    // MyTextWatcher(EditText edit, ImageButton search_btn){
    // this.edit = edit;
    // this.search_btn = search_btn;
    // }
    //
    // @Override
    // public void afterTextChanged(Editable s) {
    // String editVal = edit.getText().toString().trim();
    // if(StringUtils.isNotBlank(editVal))
    // search_btn.setVisibility(View.VISIBLE);
    // else {
    // search_btn.setVisibility(View.GONE);
    // if(qryByBtnFlag){
    // qryByBtnFlag = false;
    // listData.clear();
    // adapter.clear();
    // progressDialog = ProgressDialog.show(OpenIONetActivity.this, "",
    // "查询中...", true, true);
    //
    // new Thread(new Runnable() {
    // @Override
    // public void run() {
    // Map<String, Object> argsMap = new HashMap<String, Object>();
    // argsMap.put(KEY_pageNum, 0);
    // argsMap.put(KEY_byDeviceIP, false);
    // argsMap.put(KEY_deviceIP, null);
    // getDataFromServer(argsMap);
    // handler.sendEmptyMessage(QUERY);
    // }
    // }).start();
    // }
    // }
    // }
    //
    // @Override
    // public void beforeTextChanged(CharSequence s, int start, int count,
    // int after) {
    //
    // }
    //
    // @Override
    // public void onTextChanged(CharSequence s, int start, int before,
    // int count) {
    //
    // }
    //
    // }

    private void iniPopupWindow() {
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.title_func_popupwindow, null);
        lvPopupList = (ListView) layout.findViewById(R.id.lv_popup_list);
        pwMyPopWindow = new PopupWindow(layout);
        pwMyPopWindow.setFocusable(true);// 加上这个popupwindow中的ListView才可以接收点击事件
        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(OpenIONetActivity.this, R.layout.list_item_popupwindow, R.id.tv_list_item, getResources().getStringArray(
                R.array.openIONetQryCondition));
        lvPopupList.setAdapter(arrayAdapter);
        lvPopupList.setOnItemClickListener(new ClickPopupItemListener(arrayAdapter));
        // 控制popupwindow的宽度和高度自适应
        lvPopupList.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        pwMyPopWindow.setWidth(lvPopupList.getMeasuredWidth());
        // pwMyPopWindow.setHeight((lvPopupList.getMeasuredHeight()) * 3);
        pwMyPopWindow.setHeight(LayoutParams.WRAP_CONTENT);
        // 控制popupwindow点击屏幕其他地方消失
        pwMyPopWindow.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.bg_popupwindow));
        // 触摸popupwindow外部，popupwindow消失。这个要求你的popupwindow要有背景图片才可以成功，如上
        pwMyPopWindow.setOutsideTouchable(true);
    }

    private class ClickPopupItemListener implements AdapterView.OnItemClickListener {
        ArrayAdapter<String> arrayAdapter;

        ClickPopupItemListener(ArrayAdapter<String> arrayAdapter) {
            this.arrayAdapter = arrayAdapter;
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
            pwMyPopWindow.dismiss();
            if (loadMoreView != null)
                listView.removeFooterView(loadMoreView);
            listData.clear();
            if (position == 0) {
                curNetDurIsZero = true;
                progressDialog = ProgressDialog.show(OpenIONetActivity.this, "", "查询中...", true, true);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Map<String, Object> argsMap = new HashMap<String, Object>();
                        argsMap.put(KEY_pageNum, 0);
                        argsMap.put(KEY_byDeviceIP, false);
                        argsMap.put(KEY_deviceIP, null);
                        getDataFromServer(argsMap);
                        handler.sendEmptyMessage(QRY_NET_DUR_ZERO);
                    }
                }).start();
            } else if (arrayAdapter.getCount() - 3 == position) {
                Intent intent = new Intent(OpenIONetActivity.this, AdvanceSofterwareQueryActivity.class);
                startActivity(intent);
            } else {
                curNetDurIsZero = false;
                progressDialog = ProgressDialog.show(OpenIONetActivity.this, "", "查询中...", true, true);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Map<String, Object> argsMap = new HashMap<String, Object>();
                        argsMap.put(KEY_pageNum, 0);
                        argsMap.put(KEY_byDeviceIP, false);
                        argsMap.put(KEY_deviceIP, null);
                        getDataFromServer(argsMap);
                        handler.sendEmptyMessage(QRY_NET_DUR_NOT_ZERO);
                    }
                }).start();
            }
            Toast.makeText(OpenIONetActivity.this, "" + arrayAdapter.getItem(position).toString(), Toast.LENGTH_LONG).show();
        }
    }

    private class ListAdapter extends ListViewAdapter {
        @SuppressWarnings("unused")
        private boolean netDurIsZero;

        public void setNetDur(boolean netDurIsZero) {
            this.netDurIsZero = netDurIsZero;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            final HashMap<String, Object> hashMap = getData(position);
            if (view == null) {
                view = LayoutInflater.from(OpenIONetActivity.this).inflate(R.layout.openionet_listpage_item, null);
            }
            /**默认背景色设置*/
			view.setBackgroundResource(R.drawable.list_item_state1);
			/**双行背景色设置*/
			if (position % 2 == 0) {
				view.setBackgroundResource(R.drawable.list_item_state);
			}

            TextView tv1 = (TextView) view.findViewById(R.id.tv_depname);
            TextView tv2 = (TextView) view.findViewById(R.id.tv_devip);
            TextView tv3 = (TextView) view.findViewById(R.id.tv_devname);
            tv1.setText(hashMap.get(IP_ADDRESS).toString());
            tv2.setText(hashMap.get(VIOLATION_IP).toString());
            tv3.setText(hashMap.get(ALARM_TIME).toString());
            return view;
        }
    }

    @Override
    public void onBackPressed() {
        finish();
        overridePendingTransition(R.anim.push_right_out, R.anim.push_right_in);
        super.onBackPressed();
    }
}
